package hapiserverproject;

import javax.swing.JTextArea;

import ca.uhn.hl7v2.app.SimpleServer;
import ca.uhn.hl7v2.llp.LowerLayerProtocol;
import ca.uhn.hl7v2.parser.PipeParser;


public class Sunucu {
	
	int port; // The port to listen on
//	int portFromUser;
	LowerLayerProtocol llp; // The transport
	// protocol
	PipeParser parser; // The message parser
	SimpleServer server;
	ExampleReceiverApplication handler;
	String statusLabelString;
	JTextArea textArea;
	String s;
	
	public String getStatusLabelString() {
		return statusLabelString;
	}


	public void setStatusLabelString(String statusLabelString) {
		this.statusLabelString = statusLabelString;
	}

	
	public Sunucu() {
		// myHL7Serverdan gelen properties
//		port = portFromUser;
		port = 2100;
		llp = LowerLayerProtocol.makeLLP();
		parser = new PipeParser();
		server = new SimpleServer(port, llp, parser);
		handler = new ExampleReceiverApplication();
		server.registerApplication("ADT", "A01", handler);
		server.registerApplication("ADT", "A05", handler);
		textArea = new JTextArea();
	}
	
	public void receiveText(JTextArea theTextArea)	{
		this.textArea = theTextArea;
		handler.receiveTextToReceiver(this.textArea);
	}
	
//	public JTextArea returnTextArea(){
//		return textArea;
//	}
	
//	public String receiveString(){
//		this.s = handler.getOutput();
//		return s;
//	}
	
//	public void writeToTextArea(){
//		textArea.append(receiveString());
//	}
	
	public void startServer() {
		server.start();
	}
	
	public void stopServer() {
		server.stop();
	}
	
	public boolean checkStatus()
	{
		return server.isRunning();
	}
	
	public void statusUpdate() {
//		writeToTextArea();
//		returnTextArea();
		if (server.isRunning()) {
			setStatusLabelString("Running");
		} 
		else {
			setStatusLabelString("Not Running");
		}
	}

}
